package com.liangdemo.web.Service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangdemo.web.Service.UserService;
import com.liangdemo.web.mapper.UserMapper;
import com.liangdemo.web.model.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Boolean insert(User user) {
        return getBaseMapper().insert(user) > 0;
    }

    @Override
    public Page<User> findByPage(Integer pageNum, Integer pageSize) {
        //用插件进行分页
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.findByPage();

    }

    /**
     * 根绝用户id查询用户详情
     *
     * @param userid
     * @return
     */
    @Override
    public Object Detailed(String userid) {
        //先去查询缓存中是否存在所要查询的数据
        Object userVM =  redisTemplate.opsForValue().get(userid);

        if (userVM == null) {
            userVM = userMapper.Detailed(userid);
            redisTemplate.opsForValue().set(userid, userVM);
            return userVM;
        } else {
            return userVM;
        }

    }


}
